<template>
  <view class="page-login">
  <uni-nav-bar left-icon="back" @clickLeft="handleBack" backgroundColor="transparent" :border="false" :statusBar="true"></uni-nav-bar>
  <view class="other-login">
    <view class="code-empower">
      <view class="empower-header">
        <image class="header-logo" src="https://fine-yahe.oss-cn-shanghai.aliyuncs.com/app/empty/logo.png"></image>
        <button v-if="agree" openType="getPhoneNumber" @getphonenumber="getUserPhone">{{btnText}}</button>
        <button v-else @click="$refs.agreePopup.open()">{{btnText}}</button>
      </view>
      <button class="empower-button" @click="handleToRegister">手机号登录/注册</button>
    </view>
    <view class="code-agree box-flex">
      <image @click="agree = !agree" v-show="!agree" src="@/static/status-radio-unselected@2x.png"></image>
      <image @click="agree = !agree" v-show="agree" src="@/static/status-radio-selected@2x.png"></image>
      <p>阅读并同意<span @click="handleToAgreement('yonghu')">《用户协议》</span><span @click="handleToAgreement('yinsi')">《隐私条款》</span></p>
    </view>
    <uni-popup ref="agreePopup">
      <view class="agree-popup">
        <view class="popup-main">
          <view class="popup-h2">温馨提示</view>
          <view class="main-info">为了更好的保障您的合法权益，请您阅读并同意牙荷以下协议<span @click="handleToAgreement('')">《用户协议》</span><span @click="handleToAgreement">《隐私条款》</span></view>
        </view>
        <view class="popup-footer">
          <button class="footer-button" @click="$refs.agreePopup.close()">不同意</button>
          <button class="footer-button" openType="getPhoneNumber" @getphonenumber="getUserPhone">同意并继续</button>
        </view>
      </view>
    </uni-popup>
  </view>
  </view>
</template>

<script>
  import {
    fVailPhone
  } from '@/utils/Tools.js'
  export default {
    data() {
      return {
        agree: false,
        btnText: '手机号快捷登录'
      };
    },
    onLoad(options) {
      // #ifdef MP
      this.isMp = true
      // #endif
      /*#ifdef MP-TOUTIAO*/
      this.btnText = '抖音账号一键登录'
      /*#endif*/
    },
    methods: {
      handleBack(){  ///   返回
        uni.navigateBack({
          delta:1,
          fail: () => {
            uni.reLaunch({
              url:'/pages/index/index'
            })
          }
        })
      },
      handleToAgreement(type) { //  协议
        uni.navigateTo({
          url:`/pages/other/agreement?type=${type}`
        })
      },
      getTokenCode(){
        return new Promise(resolve => {
          // #ifdef MP-WEIXIN
          uni.login({
            success: (res) => {
              resolve(res.code)
            },
          })
          // #endif
          // #ifdef MP-TOUTIAO
          tt.login({
            success: (res) => {
              resolve(res.code)
            },
          })
          // #endif
        })
      },
      async getUserPhone(e) {
        let loginType = 'WECHAT_MINIPROGRAM'
        // #ifdef MP-WEIXIN
        const {
          encryptedData,
          iv,
          errMsg
        } = e.detail;
        if (-1 != errMsg.indexOf('deny')) {
          uni.showModal({
            title: '提示',
            content: '您已拒绝，将无法登录使用；如需登录使用，请再次点击微信用户一键登录按钮，并在微信授权窗口点击允许'
          });
          return
        }
        let data = {
          username: this.$mpAppid,
          loginType,
          password: await this.getTokenCode(),
          wechatInfoReq: {
            encryptedData,
            iv,
          },
        }
        // #endif
        // #ifdef MP-TOUTIAO
        loginType = 'DOUYIN_MOBILE_MINIPROGRAM';
        const {
          encryptedData,
          iv
        } = e.detail;
        let data = {
          username: this.$mpAppid,
          loginType,
          password: await this.getTokenCode(),
          wechatInfoReq: {
            encryptedData,
            iv,
          },
        }
        // #endif
        if (uni.getStorageSync('invite_uid')) {
          Object.assign(data, {
            salesMan: uni.getStorageSync('invite_uid')
          })
        }
        uni.showLoading({
          title:"加载中..."
        })
        const res = await this.$post('/yahe/login', data, '1')
        if (res.code == '1') {
          this.$toast('登录成功', 'success')
          uni.setStorageSync('user', {
            ...res.content,
            loginType,
            timestamp: new Date().getTime()
          });
          uni.setStorageSync('openID',res.content.openId)
          uni.setStorageSync('userSource', res.content.userSource)
          uni.setStorageSync('token', res.content.token.accessToken)
          uni.$emit('updateLogin')
          setTimeout(()=>{
            uni.reLaunch({
              url:'/pages/index/index'
            })
          },1000)
        } else {
          this.$toast(res.message, 'none')
        }
      },
      handleToRegister() { //  新用户注册
        uni.navigateTo({
          url:'/pages/other/register'
        })
      },
    }
  }
</script>

<style lang="less" scoped>
  .page-login{
    background: linear-gradient( 180deg, rgba(16, 190, 175, 0.22) 0%, #fff 700rpx);
  }
  .other-login{
    padding: 60rpx;
  }
  .empower-header {
    display: flex;
    flex-direction: column;
    align-items: center;

    .header-logo {
      width: 258rpx;
      height: 258rpx;
      margin: 60rpx 0 160rpx;
    }

    button {
      background: linear-gradient( 90deg, var(--theme-color), #10BEAF 100%);
      color: #fff;
      font-size: 30rpx;
      line-height: 88rpx;
      border-radius: 88rpx;
      width: 100%;
    }
  }

  .empower-button {
    background: #fff;
    font-size: 30rpx;
    line-height: 88rpx;
    border-radius: 88rpx;
    margin: 28rpx 0 40rpx;
    border: 1rpx solid var(--border-color);
  }

  .code-login {
    padding: 196rpx 48rpx;

    h1 {
      color: #111;
      font-size: 48rpx;
      font-weight: 400;
    }

    p {
      color: #A5A6AC;
      font-size: 28rpx;
      margin: 20rpx 0 100rpx;
    }

    .input {
      padding: 0 20rpx;
      font-size: 32rpx;
      border: 1px solid #e3e5e7;
      border-radius: 16rpx;
      color: #111;
      height: 88rpx;
      line-height: 88rpx;
      box-sizing: border-box;
      background: transparent !important;
    }
  }  //  code-login end
  
  .code-agree {
    color: #B0B0B0;
    font-size: 24rpx;
    align-items: center;
    image {
      width: 32rpx;
      height: 32rpx;
      margin-right: 10rpx;
    }
  
    p {
      margin: 0;
      min-width: 0;
      flex: 1;
      line-height: 30rpx;
    }
  
    span {
      font-weight: bold;
      color: var(--theme-color);
    }
  }
  
  .agree-popup{
    width: 80vw;
    background: #fff;
    border-radius: 20rpx;
    .popup-main{
      padding: 40rpx 48rpx;
      .main-info{
        font-size: 26rpx;
        color: #6D6D6D;
        letter-spacing: 1rpx;
        span{
          color: var(--theme-color);
        }
      }
    }
    .popup-h2{
      font-size: 36rpx;
      color: var(--title-color);
      text-align: center;
      margin-bottom: 24rpx;
      font-weight: 500;
    }
    .popup-footer{
      display: flex;
      align-items: center;
      border-top: 1rpx solid var(--border-color);
      button{
        flex: 1;
        font-size: 30rpx;
        color: var(--title-color);
        line-height: 96rpx;
        font-weight: 500;
        &:last-child{
          color: var(--theme-color);
          border-left: 1px solid var(--border-color);
        }
      }
    }
  }  /// agree-popup end

  // .login end
</style>